#include "sillawidget.h"

SillaWidget::SillaWidget(Silla* silla_, QWidget *parent) :
    QWidget(parent),silla(silla_)
{
    QVBoxLayout *main_layout = new QVBoxLayout;
    this->setLayout(main_layout);

    QHBoxLayout *curvatura_layout = new QHBoxLayout;
    QLabel *curvatura_label = new QLabel("Curvatura");
    curvatura = new QSlider(Qt::Horizontal);
    curvatura->setMinimum(-50);
    curvatura->setMaximum(100);
    curvatura->setValue(0);

    curvatura_layout->addWidget(curvatura_label);
    curvatura_layout->addWidget(curvatura);

    main_layout->addLayout(curvatura_layout);

    QHBoxLayout *grosor_layout = new QHBoxLayout;
    QLabel *grosor_label = new QLabel("Grosor");
    grosor = new QSlider(Qt::Horizontal);
    grosor->setMinimum(-10);
    grosor->setMaximum(20);
    grosor->setValue(0);

    grosor_layout->addWidget(grosor_label);
    grosor_layout->addWidget(grosor);

    main_layout->addLayout(grosor_layout);

    QHBoxLayout *altura_layout = new QHBoxLayout;
    QLabel *altura_label = new QLabel("Altura");
    altura = new QSlider(Qt::Horizontal);
    altura->setMinimum(-50);
    altura->setMaximum(100);
    altura->setValue(0);

    altura_layout->addWidget(altura_label);
    altura_layout->addWidget(altura);

    main_layout->addLayout(altura_layout);

    QHBoxLayout *color_layout = new QHBoxLayout;
    QLabel *color_label = new QLabel("Color");
    color = new QSlider(Qt::Horizontal);
    color->setMinimum(0);
    color->setMaximum(200);
    color->setValue(1);

    color_layout->addWidget(color_label);
    color_layout->addWidget(color);

    main_layout->addLayout(color_layout);

    QPushButton *save_button = new QPushButton("Guardar");
    main_layout->addWidget(save_button);

    connect(curvatura,SIGNAL(valueChanged(int)),
            this,SLOT(SetCurvatura(int)));

    connect(grosor,SIGNAL(valueChanged(int)),
            this,SLOT(SetGrosor(int)));

    connect(altura,SIGNAL(valueChanged(int)),
            this,SLOT(SetAltura(int)));

    connect(color,SIGNAL(valueChanged(int)),
            this,SLOT(SetColor(int)));

    connect(save_button,SIGNAL(clicked()),this,SLOT(SaveDialog()));
}

void SillaWidget::SaveDialog(){
    QString fileName = QFileDialog::getSaveFileName(this,tr("Save object..."),QString(),tr("BRep (*.brp)"));
    if (fileName.isEmpty())
        return;

    if(!silla->Save(fileName)){
        QErrorMessage *error = new QErrorMessage(this);
        error->showMessage("Error");
    }
}

void SillaWidget::SetCurvatura(int val){
    silla->SetCurvatura(val);
    silla->Update();
    emit Updated();
}

void SillaWidget::SetGrosor(int val){
    silla->SetGrosor(val);
    silla->Update();
    emit Updated();
}

void SillaWidget::SetAltura(int val){
    silla->SetAltura(val);
    silla->Update();
    emit Updated();
}

void SillaWidget::SetColor(int val){
    silla->SetColor(val);
    silla->Update();
    emit Updated();
}
